package com.feishi.project.javafx.excel.convert.util;

import org.yaml.snakeyaml.Yaml;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2020-11-03
 * Time: 上午10:26
 */
public class YamlUtil {
    public static Map load(String fileName){
        Yaml yaml = new Yaml();
        return yaml.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
    }

    public static  <T> T getConfigValue(Map map,String keys, T defaultValue, Class<T> tClass) {
        Object result = getConfig(map,keys);
        if (tClass.isInstance(result)) {
            return (T) result;
        } else {
            return defaultValue;
        }
    }
    public static Object getConfig(Map map,String keys) {
        if (keys == null) {
            return map;
        }
        Object result = map;
        for (String key : keys.split("\\.")) {
            result = ((Map) result).get(key);
            if (result != null && result instanceof Map) {
                continue;
            }
            break;
        }

        return result;
    }
}
